package com.example.demowx.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demowx.utils.MyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author pengyifeng
 * @version 1.0.0
 * @ClassName DemoGetUserInfoController.java
 * @Description 获取未关注用户信息
 * @createTime 2020年11月25日 17:17:00
 */
@RestController
public class DemoGetUserInfoController {

    @Value("${demo.appID}")
    private String appId;

    @Value("${demo.appsecret}")
    private String appsecret;

    @GetMapping("/GetUserInfo")
    public void getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取code
        String code = request.getParameter("code");
        //换取accesstoken的地址
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        url = url.replace("APPID", appId).replace("SECRET", appsecret).replace("CODE", code);
        String result = MyUtil.get(url);
        String at = JSONObject.parseObject(result).getString("access_token");
        String openid = JSONObject.parseObject(result).getString("openid");
        //拉取用户的基本信息
        url="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        url=url.replace("ACCESS_TOKEN", at).replace("OPENID", openid);
        result = MyUtil.get(url);
        System.out.println(result);



    }
}
